import webview

url = 'https://cloud.huawei.com/#/notepad/note/'

el_selector = '#notepadCompleteIcon'

js = """

// 监听键盘
document.addEventListener('keydown', function (event) {
    // ctrl+t
    if (event.ctrlKey && event.key === 't') {
        on_ctrl_t();
        event.preventDefault();
    }
});

// 当按下ctrl+t
function on_ctrl_t() {
    var el = document.querySelector('#notepadCompleteIcon > .svg-icon')
    if(el){
        el.dispatchEvent(new MouseEvent('click', {
            'view': window,
            'bubbles': true,
            'cancelable': true
        }))
    }
}
"""


def on_stared():
    print("启动完成")


def on_loaded(window):
    current_url: str = window.get_current_url()
    print('DOM is ready ', current_url)
    if current_url.startswith("https://cloud.huawei.com/home"):
        window.evaluate_js(js)


if __name__ == '__main__':
    window = webview.create_window('note', url, width=1200, height=800)
    window.events.loaded += on_loaded
    webview.start(on_stared)
